﻿using PokemonBattle.BattleRoom.Client;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PBO.Forms.NetworkForm
{
    public partial class AdvanceLinkForm : Form
    {
        public AdvanceLinkForm()
        {
            InitializeComponent();
        }

        private User _userInfo;

        public AdvanceLinkForm(User user)
        {
            InitializeComponent();

            this.Icon = Properties.Resources.PokemonBall;

            if (Properties.Settings.Default.RoomAddress == null)
            {
                Properties.Settings.Default.RoomAddress = new System.Collections.Specialized.StringCollection();
            }

            foreach (string item in Properties.Settings.Default.RoomAddress)
            {
                AddressCombo.Items.Add(item);
            }
            _userInfo = user;
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            var roomAddress = Properties.Settings.Default.RoomAddress;

            var address = AddressCombo.Text.Trim();
            if (Regex.IsMatch(address, @"^[\.\w]+$", RegexOptions.Compiled))
            {
                if (!roomAddress.Contains(address))
                {
                    roomAddress.Add(address);
                    if (roomAddress.Count > 10) roomAddress.RemoveAt(0);
                }
                MainForm.GetInstance().BuildRoomUserForm(_userInfo, AddressCombo.Text, AddressCombo.Text).Show();
                Close();
            }
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
